package cn.gjxblog.controller;

import cn.gjxblog.api.AgencyServerFegin;
import cn.gjxblog.api.HouseServerFegin;
import cn.gjxblog.api.MailServiceFegin;
import cn.gjxblog.api.UserServiceFegin;
import cn.gjxblog.common.model.HttpResult;
import cn.gjxblog.common.model.PageCondition;
import cn.gjxblog.common.model.PageData;
import cn.gjxblog.model.*;
import cn.gjxblog.model.dto.HouseQueryDTO;
import cn.gjxblog.model.entity.Agency;
import cn.gjxblog.model.entity.House;
import cn.gjxblog.model.entity.User;
import cn.gjxblog.model.entity.UserMsg;
import cn.gjxblog.model.vo.HouseVO;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Objects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author gjx
 * @version v1.0
 * @Description
 * @date 2018/12/18 14:51
 */
@Controller
@RequestMapping("/agency")
public class AgencyController {

    @Autowired
    private AgencyServerFegin agencyServerFegin;

    @Autowired
    private HouseServerFegin houseServerFegin;

    @Autowired
    private MailServiceFegin mailServiceFegin;

    @Autowired
    private UserServiceFegin userServiceFegin;

    @RequestMapping("/create")
    public String agencyCreate(){
        User user =  UserContext.getUser();
        if (user == null || !Objects.equal(user.getEmail(), "spring_boot@163.com")) {
            return "redirect:/accounts/signin?" + ResultMsg.errorMsg("请先登录").asUrlParams();
        }
        return "/user/agency/create";
    }
    @RequestMapping("/submit")
    public String agencySubmit(Agency agency){
        User user =  UserContext.getUser();
        if (user == null || !Objects.equal(user.getEmail(), "spring_boot@163.com")) {
            return "redirect:/accounts/signin?" + ResultMsg.errorMsg("请先登录").asUrlParams();
        }
        agencyServerFegin.addAgency(agency);
        return "redirect:/index?" + ResultMsg.errorMsg("创建成功").asUrlParams();
    }

    @RequestMapping("/list")
    public String agencyList(ModelMap modelMap){
        List<Agency> agencies = agencyServerFegin.queryAgency().getResult();
        List<HouseVO> houses =  queryHotHouses();
        modelMap.put("recomHouses", houses);
        modelMap.put("agencyList", agencies);
        return "/user/agency/agencyList";
    }

    @RequestMapping("/agentList")
    public String agentList(PageCondition pageCondition, User user, ModelMap modelMap){
        pageCondition.setPageSize(6);

        PageInfo<User> result = agencyServerFegin.queryAgent(pageCondition).getResult();
        PageData<User> ps = PageData.buildPage(result.getList(), result.getTotal(), pageCondition.getPageSize(),pageCondition.getPageNum());

        List<HouseVO> houses =  queryHotHouses();
        modelMap.put("recomHouses", houses);
        modelMap.put("ps", ps);
        return "/user/agent/agentList";
    }

    @RequestMapping("/agentDetail")
    public String agentDetail(String id,ModelMap modelMap){
        User user =  agencyServerFegin.getAgent(id).getResult();
        List<HouseVO> houses =  queryHotHouses();

        PageCondition pageCondition = new PageCondition(6, 1);
        HouseQueryDTO houseQueryDTO = new HouseQueryDTO();
        houseQueryDTO.setPageCondition(pageCondition);
        houseQueryDTO.setUserId(id);
        PageInfo result = houseServerFegin.queryHousesByUserId(houseQueryDTO).getResult();

        PageData<House> bindHouse = PageData.buildPage(result.getList(), result.getTotal(), pageCondition.getPageSize(),pageCondition.getPageNum());

        if (bindHouse != null) {
            modelMap.put("bindHouses", bindHouse.getList()) ;
        }

        modelMap.put("recomHouses", houses);
        modelMap.put("agent", user);
        return "/user/agent/agentDetail";
    }

    @RequestMapping("/agencyDetail")
    public String agencyDetail(String id,ModelMap modelMap){
        Agency agency =  agencyServerFegin.getAgency(id).getResult();

        List<HouseVO> houses =  queryHotHouses();
        modelMap.put("recomHouses", houses);
        modelMap.put("agency", agency);
        return "/user/agency/agencyDetail";
    }


    @RequestMapping("/agentMsg")
    public String agentMsg(String agentId,String msg, ModelMap modelMap){
        UserMsg userMsg = new UserMsg();
        userMsg.setUserId(UserContext.getUser().getId());
        userMsg.setUserPhone(UserContext.getUser().getPhone());
        userMsg.setAgentId(agentId);
        userMsg.setMsg(msg);
        userServiceFegin.addUserMsg(userMsg);
        return "redirect:/agency/agentDetail?" + ResultMsg.successMsg("留言成功").asUrlParams();
    }


    /**
     * 获取热度排行榜
     * @return
     */
    private List<HouseVO> queryHotHouses() {
        HttpResult<List<HouseVO>> restHouses = houseServerFegin.queryHotHouses(CommonConstants.RECOM_SIZE);
        List<HouseVO> rcHouses =restHouses.getResult();
        return  rcHouses;
    }

}
